package com.erpanderp.wam.usm.view.leave;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.adm.model.entity.Role;
import com.erpanderp.wam.usm.model.entity.leave.EmployeeAttendance;
import com.erpanderp.wam.usm.model.entity.leave.LeaveBalance;

import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeRole;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.leave.LeaveBalanceDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeRoleDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.WAMUtil;


@ViewScoped
@ManagedBean
public class EmployeeLeaveBalances {
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	
	private Map<String, Integer> monthsMap = CommonLookup.getMonths();
	private Map<String, Integer> yearsMap = WAMUtil.getLastSixYears();
	private Map<String, String> modulesMap = new HashMap<String, String>();
	private Map<String, String> leaveStatusMap = CommonLookup.getLeaveStatus();
	private Integer selectedMonth;
	private Integer selectedYear;
	private List<LeaveBalance> leaveBalances = new ArrayList<LeaveBalance>();
	LeaveBalanceDAO leaveBalanceDAO = new LeaveBalanceDAO();
	private boolean RENDERTABLE;
	private boolean RENDER_NO_RESULTS;
	EmployeeDAO employeeDAO = new EmployeeDAO();
	private List<EmployeeAttendance> employeeAttendances = new ArrayList<EmployeeAttendance>();
	private WAMUser wamUser = new WAMUser();
	EmployeeRoleDAO employeeRoleDAO = new EmployeeRoleDAO();
	private List<Employee> employees;
	public EmployeeLeaveBalances(){
		System.out.println("IN EMPLOYEE LEAVEBALANCE CONSTRUCTOR");
		wamUser = ((WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER));
		Calendar calendar = Calendar.getInstance();
		selectedMonth = calendar.get(Calendar.MONTH);
		System.out.println("SELECTED MONTH ="+selectedMonth);
		selectedYear = calendar.get(Calendar.YEAR);
	}
	
	public void searchActionListener(){
		List<Employee> employees = new ArrayList<Employee>();
		employeeAttendances.clear();
		employees = employeeDAO.getAllEmployees();
		
		leaveBalances = leaveBalanceDAO.getLeaveBalances();
		for(Employee employee : employees){
			EmployeeAttendance employeeAttendance = new EmployeeAttendance();
			 
			if(!employee.getEmployeeCode().equals("admin") && (!employee.getEmployeeCode().equals("adminhr") && !employee.getEmployeeCode().equals("A00001"))){
			
			leaveBalances = (List<LeaveBalance>) leaveBalanceDAO.getLeaveBalancesList(employee.getEmployeeId());
			for(LeaveBalance leaveBalance : leaveBalances){
				
				employeeAttendance.setEmployee(leaveBalance.getEmployee());
				if(leaveBalance != null && leaveBalance.getLeaveType().getLeaveType().equals(CommonLookup.CASUALLEAVE_TYPE)){
				 employeeAttendance.setCLBalance(leaveBalance.getBalance());
				}else if(leaveBalance != null && leaveBalance.getLeaveType().getLeaveType().equals(CommonLookup.SICKLEAVE_TYPE)){
				 employeeAttendance.setSLBalance(leaveBalance.getBalance());	
				}else if(leaveBalance != null && leaveBalance.getLeaveType().getLeaveType().equals(CommonLookup.PRIVILIZEDLEAVE_TYPE)){
				 employeeAttendance.setPLBalance(leaveBalance.getBalance());	
				}else if(leaveBalance != null && leaveBalance.getLeaveType().getLeaveType().equals(CommonLookup.WORKFROMHOME_TYPE)){
					employeeAttendance.setWHBalance(leaveBalance.getBalance());
				}else if(leaveBalance != null && leaveBalance.getLeaveType().getLeaveType().equals(CommonLookup.LOSS_OF_PAY_TYPE)){
					employeeAttendance.setLOPBalance(leaveBalance.getBalance());
				}else if(leaveBalance != null && leaveBalance.getLeaveType().getLeaveType().equals(CommonLookup.COMP_OF_TYPE)){
					employeeAttendance.setCOMPOFFBalance(leaveBalance.getBalance());
				}else{
					employeeAttendance.setOHBalance(leaveBalance.getBalance());
				}
				
			}
			employeeAttendances.add(employeeAttendance);
			System.out.println("ATTENDANCE LIST SIZE IS "+employeeAttendances.size());
			
			//leaveBalanceReport.setClBal(leaveBalance)
		}
		if(leaveBalances != null && leaveBalances.size() > 0){
			RENDERTABLE = true;
			RENDER_NO_RESULTS = false;
		}else{
			RENDERTABLE = false;
			RENDER_NO_RESULTS = true;
		}
		}
	}

	public Map<String, Integer> getMonthsMap() {
		return monthsMap;
	}

	public void setMonthsMap(Map<String, Integer> monthsMap) {
		this.monthsMap = monthsMap;
	}

	public Map<String, Integer> getYearsMap() {
		return yearsMap;
	}

	public void setYearsMap(Map<String, Integer> yearsMap) {
		this.yearsMap = yearsMap;
	}

	public Map<String, String> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, String> modulesMap) {
		this.modulesMap = modulesMap;
	}

	public Map<String, String> getLeaveStatusMap() {
		return leaveStatusMap;
	}

	public void setLeaveStatusMap(Map<String, String> leaveStatusMap) {
		this.leaveStatusMap = leaveStatusMap;
	}

	public Integer getSelectedMonth() {
		return selectedMonth;
	}

	public void setSelectedMonth(Integer selectedMonth) {
		this.selectedMonth = selectedMonth;
	}

	public Integer getSelectedYear() {
		return selectedYear;
	}

	public void setSelectedYear(Integer selectedYear) {
		this.selectedYear = selectedYear;
	}

	public List<LeaveBalance> getLeaveBalances() {
		return leaveBalances;
	}

	public void setLeaveBalances(List<LeaveBalance> leaveBalances) {
		this.leaveBalances = leaveBalances;
	}

	public boolean isRENDERTABLE() {
		return RENDERTABLE;
	}

	public void setRENDERTABLE(boolean rENDERTABLE) {
		RENDERTABLE = rENDERTABLE;
	}

	public boolean isRENDER_NO_RESULTS() {
		return RENDER_NO_RESULTS;
	}

	public void setRENDER_NO_RESULTS(boolean rENDER_NO_RESULTS) {
		RENDER_NO_RESULTS = rENDER_NO_RESULTS;
	}

	public List<EmployeeAttendance> getEmployeeAttendances() {
		return employeeAttendances;
	}

	public void setEmployeeAttendances(List<EmployeeAttendance> employeeAttendances) {
		this.employeeAttendances = employeeAttendances;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}
	

}
